<?php
namespace Beerus\Modules\Designer\Actions\SaleSend;

use Doraemon\cache\Queue;
use Doraemon\model\Designer\DesignerAuthSendMsg;
use Doraemon\model\Sysmsg;
use Doraemon\service\User;
use Doraemon\tools\HhzNiceLivingUrl;
use Doraemon\tools\Log;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Widgets\StepForm;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;

class Submit extends StepForm
{
    public $title = "确认信息";

    const SEND_INFO_LINK = [
        1 => 'https://m.haohaozhu.cn/designer/certification?designer_type=1',
        2 => 'https://m.haohaozhu.cn/designer/certification?designer_type=2',
        3 => 'https://m.haohaozhu.cn/designer/certification?designer_type=3',
        4 => 'https://m.haohaozhu.cn/community/decoration-company-certification',
    ];

    public function handle(Request $request)
    {
        $all = $this->all();
        $info = $all['info'] ?: [];
        if (!$info) {
            $error = new MessageBag([
                'title' => '信息有误'
            ]);
            return back()->with(compact('error'));
        }

        $this->sendMsg($info);
        $this->clear();
        $success = new MessageBag([
            'title' => '发送成功'
        ]);
        return redirect('/admin/Designer/sale/sendList')->with(compact('success'));
    }

    public function form()
    {
        $all = $this->all();
        $uid = $all['info']['to_uid'] ?: 0;
        $nick = $all['info']['nick'] ?: '';
        $type = $all['info']['type'] ?: 0;

        $send_time = DesignerAuthSendMsg::getLastMsgTimeByMsgCode($uid, DesignerAuthSendMsg::MSG_CODE);

        $html = "";
        if ($send_time) {
            $html .= "<p>最近一次的发送时间：<span>{$send_time}</span></p>";
        } else {
            $html .= "<p>最近一次的发送时间：<span>无</span></p>";
        }

        $send_template = Info::SEND_TEMPLATE;
        $html .= "<h5 style='color: red'>确认发送信息:</h5>";
        $html .= "<p>发送人UID：<span>{$uid}</span></p>";
        $html .= "<p>发送人昵称：<span>{$nick}</span></p>";
        $html .= "<p>发送的模板：<span>{$send_template[$type]}</span></p>";

        $this->html($html);
    }

    public function sendMsg($params)
    {
        $uid = $params['to_uid'];
        $nick = $params['nick'];
        $type = $params['type'];
        Log::info("sale_designer_info", $params);
        $link = self::SEND_INFO_LINK[$type] ?: '';
        $message = sprintf("Hi %s，欢迎入驻好好住！【点击此处】上传认证资料，工作人员将在一个工作日内处理你的资料。", $nick);

        // 入发送消息表
        $insertData = [
            'to_uid' => $uid,
            'from_uid' => User::DEFAULT_HAOHAOZHU_UID,
            'template_type' => $type,
            'link' => $link,
            'send_time' => date("Y-m-d H:i:s"),
            'admin_name' => Admin::user()->username,
            'admin_id' => Admin::user()->id,
            'msg_code' => DesignerAuthSendMsg::MSG_CODE
        ];
        DesignerAuthSendMsg::insert($insertData);

        // 发送下行消息
        $message_data = [
            'msgCode' => DesignerAuthSendMsg::MSG_CODE,
            'to_uid' => $uid,
            'msgParams' => [
                'from_uid' => User::DEFAULT_HAOHAOZHU_UID,
                'title' => '好好住认证资料提交入口',
                'detail' => $message,
                'link' => HhzNiceLivingUrl::getNormalH5Link($link)
            ]
        ];
        Queue::insertQueue('queue/Sysmsg/Send', $message_data, Queue::QUEUE_DORAEMON);

        Log::info("sale_designer_send", $message_data);
    }
}